package com.phkj.qdh.ui.dialog

import android.content.Context
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.view.ShapeButton
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.core.CenterPopupView
import com.phkj.qdh.R
import com.phkj.qdh.bean.DeptTreeVoList
import com.phkj.qdh.bean.DeptUserVo
import com.phkj.qdh.ui.adapter.EmployeeDepartmentAdapter
import com.phkj.qdh.ui.adapter.employeenode.EmployeeHeadNode
import com.phkj.qdh.ui.adapter.employeenode.EmployeeNode
import com.phkj.qdh.ui.adapter.employeenode.EmployeeNodeAdapter
import com.phkj.qdh.utils.LogUtil
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.node.BaseNode
import rxhttp.wrapper.utils.GsonUtil

class EmployeeSelectDialog(context: Context, val list: ArrayList<DeptTreeVoList>) :
    CenterPopupView(context) {
    private var title: String = ""
    private val employeeAdapter by lazy { EmployeeNodeAdapter() }
    fun setTitle(s: String) {
        this.title = s
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_select_man
    }

    override fun onCreate() {
        super.onCreate()

        val tvTitle = findViewById<TextView>(R.id.tv_title)
        val rv = findViewById<RecyclerView>(R.id.rv_item)
        rv.run {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(MyItemDecoration(width = 5))
            adapter = employeeAdapter
        }
        tvTitle.text = "责任人"

        val nodeThreeList = ArrayList<BaseNode>()
        list.forEachIndexed { _, deptTreeVoList1 ->
            val employeeNodeList1 = ArrayList<BaseNode>()
            deptTreeVoList1.deptUserVoList?.forEachIndexed { index, deptUserVo ->
                val employeeNode = EmployeeNode(deptUserVo)
                employeeNodeList1.add(employeeNode)
            }

            val employeeHeadNode1 = EmployeeHeadNode(deptTreeVoList1, employeeNodeList1)
            nodeThreeList.add(employeeHeadNode1)

            deptTreeVoList1.deptTreeVoList?.forEachIndexed { index, deptTreeVoList2 ->
                val employeeNodeList2 = ArrayList<BaseNode>()
                deptTreeVoList2?.deptUserVoList?.forEachIndexed { index, deptUserVo2 ->
                    val employeeNode = EmployeeNode(deptUserVo2)
                    employeeNodeList2.add(employeeNode)
                }
                val employeeHeadNode = EmployeeHeadNode(deptTreeVoList2, employeeNodeList2)
                nodeThreeList.add(employeeHeadNode)
                deptTreeVoList2.deptTreeVoList?.forEachIndexed { index, deptTreeVoList3 ->
                    val employeeNodeList3 = ArrayList<BaseNode>()
                    deptTreeVoList3?.deptUserVoList?.forEachIndexed { index, deptUserVo3 ->
                        val employeeNode = EmployeeNode(deptUserVo3)
                        employeeNodeList3.add(employeeNode)
                    }
                    val employeeHeadNode3 = EmployeeHeadNode(deptTreeVoList3, employeeNodeList3)
                    nodeThreeList.add(employeeHeadNode3)
                    deptTreeVoList3?.deptTreeVoList?.forEachIndexed { index, deptTreeVoList4 ->
                        val employeeNodeList4 = ArrayList<BaseNode>()
                        deptTreeVoList4?.deptUserVoList?.forEachIndexed { index, deptUserVo4 ->
                            val employeeNode = EmployeeNode(deptUserVo4)
                            employeeNodeList4.add(employeeNode)
                        }
                        val employeeHeadNode4 = EmployeeHeadNode(deptTreeVoList3, employeeNodeList4)
                        nodeThreeList.add(employeeHeadNode4)

                    }
                }
            }


        }
        employeeAdapter.setList(nodeThreeList)
        findViewById<ShapeButton>(R.id.btn_confirm).setOnClickListener {
//            val selected = mutableListOf<DeptUserVo>()
//            list.forEach { tree->
//                tree.
//            }
            val selectedEmployees = ArrayList<DeptUserVo>()
            employeeAdapter.data.forEach {
                if (it is EmployeeNode) {
                    if (it.isSelected) {
                        selectedEmployees.add(it.tree)
                    }
                }
            }
            if (selectedEmployees.isNotEmpty()) {
                employeeSelectListener?.ok(selectedEmployees)
                dismiss()
            } else {
                ToastUtils.show("请选择责任人")
            }

            LogUtil.E(GsonUtil.toJson(selectedEmployees))
        }
    }


    private var employeeSelectListener: EmployeeSelectListener? = null
    fun setListener(listener: EmployeeSelectListener) {
        this.employeeSelectListener = listener
    }

}

interface EmployeeSelectListener {
    fun ok(list: ArrayList<DeptUserVo>)
}